<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="图书标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入图书标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书作者" prop="author">
        <el-input
          v-model="queryParams.author"
          placeholder="请输入图书作者"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出版社" prop="publisher">
        <el-input
          v-model="queryParams.publisher"
          placeholder="请输入出版社"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="国际标准书号" prop="isbn">
        <el-input
          v-model="queryParams.isbn"
          placeholder="请输入国际标准书号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="图书类别" prop="categoryId">
        <el-select v-model="queryParams.categoryId" placeholder="请选择图书类别" clearable>
          <el-option
            v-for="dict in category_id"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['books:books:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['books:books:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['books:books:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['books:books:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="booksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="图书ID，唯一标识一本图书" align="center" prop="bookId" />
      <el-table-column label="图书标题" align="center" prop="title" />
      <el-table-column label="图书作者" align="center" prop="author" />
      <el-table-column label="出版社" align="center" prop="publisher" />
      <el-table-column label="出版日期" align="center" prop="publicationDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="国际标准书号" align="center" prop="isbn" />
      <el-table-column label="图书类别" align="center" prop="categoryId">
        <template #default="scope">
          <dict-tag :options="category_id" :value="scope.row.categoryId"/>
        </template>
      </el-table-column>
      <el-table-column label="总藏书量" align="center" prop="totalCopies" />
      <el-table-column label="可用藏书量" align="center" prop="availableCopies" />
      <el-table-column label="书架位置" align="center" prop="shelfLocation" />
      <el-table-column label="图书简介" align="center" prop="summary" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['books:books:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['books:books:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改图书信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="booksRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图书标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入图书标题" />
        </el-form-item>
        <el-form-item label="图书作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入图书作者" />
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher" placeholder="请输入出版社" />
        </el-form-item>
        <el-form-item label="出版日期" prop="publicationDate">
          <el-date-picker clearable
            v-model="form.publicationDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出版日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="国际标准书号" prop="isbn">
          <el-input v-model="form.isbn" placeholder="请输入国际标准书号" />
        </el-form-item>
        <el-form-item label="图书类别" prop="categoryId">
          <el-select v-model="form.categoryId" placeholder="请选择图书类别">
            <el-option
              v-for="dict in category_id"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="总藏书量" prop="totalCopies">
          <el-input v-model="form.totalCopies" placeholder="请输入总藏书量" />
        </el-form-item>
        <el-form-item label="可用藏书量" prop="availableCopies">
          <el-input v-model="form.availableCopies" placeholder="请输入可用藏书量" />
        </el-form-item>
        <el-form-item label="书架位置" prop="shelfLocation">
          <el-input v-model="form.shelfLocation" placeholder="请输入书架位置" />
        </el-form-item>
        <el-form-item label="图书简介" prop="summary">
          <el-input v-model="form.summary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Books">
import { listBooks, getBooks, delBooks, addBooks, updateBooks } from "@/api/books/books"

const { proxy } = getCurrentInstance()
const { category_id } = proxy.useDict('category_id')

const booksList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    author: null,
    publisher: null,
    isbn: null,
    categoryId: null,
  },
  rules: {
    title: [
      { required: true, message: "图书标题不能为空", trigger: "blur" }
    ],
    author: [
      { required: true, message: "图书作者不能为空", trigger: "blur" }
    ],
    totalCopies: [
      { required: true, message: "总藏书量不能为空", trigger: "blur" }
    ],
    availableCopies: [
      { required: true, message: "可用藏书量不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询图书信息列表 */
function getList() {
  loading.value = true
  listBooks(queryParams.value).then(response => {
    booksList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    bookId: null,
    title: null,
    author: null,
    publisher: null,
    publicationDate: null,
    isbn: null,
    categoryId: null,
    totalCopies: null,
    availableCopies: null,
    shelfLocation: null,
    summary: null
  }
  proxy.resetForm("booksRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.bookId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加图书信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _bookId = row.bookId || ids.value
  getBooks(_bookId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改图书信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["booksRef"].validate(valid => {
    if (valid) {
      if (form.value.bookId != null) {
        updateBooks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBooks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _bookIds = row.bookId || ids.value
  proxy.$modal.confirm('是否确认删除图书信息编号为"' + _bookIds + '"的数据项？').then(function() {
    return delBooks(_bookIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('books/books/export', {
    ...queryParams.value
  }, `books_${new Date().getTime()}.xlsx`)
}

getList()
</script>
